package com.javaweb.task;

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.javaweb.base.BaseService;
import com.javaweb.config.redis.RedisMqReceive;

/**
 * 系统启动时的任务
 * <p>
 * 可以有多个类实现CommandLineRunner
 * 
 * @author tank2140896
 */
@Order(value=1)
@Component
public class OnStartupTask extends BaseService implements CommandLineRunner {
	
	/**
	 * 运行方法
	 */
	@Override
    public void run(String... args) {
    	new Thread(()->this.onStartupTask_config()).start();
    	new Thread(()->this.onStartupTask_dictionary()).start();
    	new Thread(()->this.onStartupTask_interfaces()).start();
    }
    
	/**
	 * 加载配置表信息
	 */
    private void onStartupTask_config() {
		RedisMqReceive.updateConfigInfo(configService.selectAll());
		log.info("[将配置表数据加载进内存]执行完毕");
    }
    
	/**
	 * 加载字典表信息
	 */
    private void onStartupTask_dictionary() {
		RedisMqReceive.updateDictionaryInfo(dictionaryService.selectAll());
		log.info("[将字典表数据加载进内存]执行完毕");
    }
    
	/**
	 * 加载并处理接口相关信息
	 */
    private void onStartupTask_interfaces() {
    	interfacesService.synchronizedInterfaces();
		log.info("[同步数据库中的接口信息表]执行完毕");
		RedisMqReceive.updateInterfacesInfo(interfacesService.getAll());
    	log.info("[将所有接口信息加载进内存]执行完毕");
    	interfacesService.synchronizedRedisInterfaceHistoryTimes();
    	log.info("[同步redis中的各个接口调用次数]执行完毕");
    }
    
}
